package com.atguigu.vod.service.serviceImpl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.servicebase.exceptionhandler.GuiliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utis.ConstantPropertiesUtils;
import com.atguigu.vod.utis.InifVodClient;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadAliyunVod(MultipartFile file) {
        try {
            String fileName =file.getOriginalFilename();
            String title =fileName.substring(0,fileName.lastIndexOf("."));
            InputStream inputStream=file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantPropertiesUtils.KEY_ID,ConstantPropertiesUtils.KEY_SECRET, title, fileName, inputStream);
            // request.setEcsRegionId("cn-shanghai");
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
            return videoId;
        }catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void removerDuoVideo(List<String> videoIdList) {
        try {
            DefaultAcsClient client = InifVodClient.initVodClient(ConstantPropertiesUtils.KEY_ID,ConstantPropertiesUtils.KEY_SECRET);
            //创建删除视频的reauest对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            String videoId = StringUtil.join(videoIdList.toArray(),",");
            //向request中设置id
            request.setVideoIds(videoId);
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            throw  new GuiliException(20001,"删除失败！！！");
        }
    }

    @Override
    public String getPlayAuth(String vidoeId) throws ClientException {
            //根据视频id获取视频凭证
            DefaultAcsClient client = InifVodClient.initVodClient("LTAI5t8vWgvuL85cp2PMc2mU", "VMZuzEkU2mJIwZeTAVOr2oQfPVANTE");
            //获取视频凭证的request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //向requset中设置视频id
            request.setVideoId(vidoeId);
            //调用初始化对象的方法得到加密视频的凭证
            response = client.getAcsResponse(request);
            return response.getPlayAuth();
    }
}
